/**
 * Created by root on 2017-08-21.
 */
import React, { Component, PropTypes } from 'react';
import { Utility, Refresh, TaskItem, ApiInfo } from 'components';
import { connect } from 'react-redux';
import * as CommonActions from 'redux/modules/reduxCommon';

@connect(
  state => ({
    Title: state.Common.Title,                                          // 标题
    UrlParams: state.Common.UrlParams,                                  // URL参数
    TaskAssignListInfo: state.Common.TaskAssignListInfo                 // 任务分配列表数据
  }),
  { ...CommonActions })

export default class TaskAssign extends Component {
  static propTypes = {
    Title: PropTypes.string,                                            // 标题
    UrlParams: PropTypes.object,                                        // url 参数
    TaskAssignListInfo: PropTypes.object,                               // 任务分配列表数据
    onApiGet: PropTypes.func,                                           // Get请求
    onSetContent: PropTypes.func,                                       // 保存数据
  };

  constructor(props) {
    super(props);
    this.state = {
      RefreshComplete: true,
      NextDataComplete: true,
      __Index: 0,
      IsNoteData: false,                                                 // 判断有无数据
    };
  }
  componentWillMount() {
    // this.__TaskListData();
    // setTimeout(() => {
    //   this.__InitData();
    // }, 0);
    const { TaskAssignListInfo } = this.props;
    const { List } = TaskAssignListInfo || {};
    if (!Utility.isArray(List)) {
      this.__InitData({}, false);
    }
  }

  componentDidMount() {
    this.state.IsMount = true;
    // const { TaskAssignListInfo } = this.props;
    // const { List } = TaskAssignListInfo || {};
    // if (!Utility.isArray(List)) {
    //   this.__InitData({}, false);
    // }
  }

  componentWillUnmount() {
    delete this.state.IsMount;
  }

  /**
   * 初始化页面数据
   * @returns 
   * @memberof TaskAssign
   */
  __InitData(condition, isNextData) {
    const { onApiGet, TaskAssignListInfo } = this.props;
    if (!Utility.isFunction(onApiGet)) {
      return;
    }
    const __params = Object.assign({ pg_index: 0, pg_count: 16 }, condition || {});
    const __self = this;
    const __ApiList = [];
    const { List } = TaskAssignListInfo || {};
    if (document.body.scrollTop === 0 || !Utility.isArray(List) || !!isNextData) {
      __ApiList.push(onApiGet('TaskAssignListInfo', ApiInfo.JXKB.TaskAssigns, { params: __params }));
    }
    // Utility.$loading();
    Promise.all(__ApiList).then(() => {
      __self.__JudgeIsExistData();
      __self.state.RefreshComplete = true;
      __self.state.NextDataComplete = true;
      __self.__UpdateRender();
      Utility.$loadingHide();
    }).catch((ex) => {
      __self.__JudgeIsExistData();
      __self.state.RefreshComplete = true;
      __self.state.NextDataComplete = true;
      __self.__UpdateRender();
      Utility.$loadingHide();
      console.log(ex);
    });
  }

  /**
   * 判断有无数据列表
   * 
   * @returns 
   * @memberof TaskAssign
   */
  __JudgeIsExistData() {
    const { TaskAssignListInfo } = this.props;
    if (TaskAssignListInfo) {
      if (Utility.isArray(TaskAssignListInfo.List)) {
        return;
      }
    }

    this.state.IsNotData = true;
  }

  /**
   * 上拉加载
   * @memberof TaskAssign
   */
  __HandlerNextData() {
    const { TaskAssignListInfo } = this.props;
    const { Condition } = TaskAssignListInfo;
    const { IsExistsNextData } = Condition;
    if (!IsExistsNextData) {
      return;
    }
    this.state.NextDataComplete = false;
    this.__UpdateRender();
    this.__InitData(Condition, true);
  }

  /**
   * 下拉刷新
   * @memberof TaskAssign
   */
  __HandlerRefreshData() {
    const __params = Object.assign({pg_index: 0, pg_count: 16});
    this.state.RefreshComplete = false;
    this.__UpdateRender();
    this.__InitData(__params, false);
  }

  /**
   * 更新渲染
   */
  __UpdateRender() {
    if (!!this.state.IsMount) {
      this.setState({ __CurrentTempDate: new Date().getTime() });
    }
  }

  /**
   * 页面跳转。
   * @param params
   * @private TaskAssign
   */
  __HandlerGoToPage(url, params) {
    Utility.toPage(url, params);
  }

  /**
   * 任务列表
   * 
   * @param {any} styles 
   * @returns 
   * @memberof TaskAssign
   */
  __TaskAssignListHtml(styles) {
    const { TaskAssignListInfo } = this.props;
    const { List } = TaskAssignListInfo || {};
    if (!Utility.isArray(List)) {
      return null;
    }
    let goIng = [];
    let unTreat = [];
    return List.map((item) => {
      const { userName, statusNums, userId } = item || {};
      const _TaskItems = {};
      // 进行中
      goIng = statusNums.filter((go) => {
        return go.status === 'RECEIVED';
      });
      // 待处理
      unTreat = statusNums.filter((treat) => {
        return treat.status === 'START';
      });
      _TaskItems.userName = userName;
      _TaskItems.goIng = goIng;
      _TaskItems.unTreat = unTreat;
      return (
        <div className={styles.itemCss} key={userId} onClick={this.__HandlerToSearch.bind(this, userName)}>
          <TaskItem TaskItems={_TaskItems} />
        </div>
      );
    });
  }

  /**
   * 跳转-搜索页面 搜索条件: userName && status: [RECEVIED,START]
   * @param {*} url 
   * @param {*} params 
   */
  __HandlerToSearch(userName) {
    const { UrlParams, onSetContent } = this.props;
    const { token } = UrlParams || {};
    let __token = '';
    if (token) {
      __token = token;
    }
    const obj = {};
    obj.userName = userName;
    obj.type = 'wo@';
    obj.status = ['RECEIVED', 'START'];
    onSetContent('TaskAssignCondition', obj);
    const { SearchPage } = Utility.constItem.UrlItem;
    if (SearchPage) {
      Utility.toPage(SearchPage, { token: __token });
    }
  }

  render() {
    const styles = require('./scss/TaskAssign.scss');
    const { RefreshComplete, NextDataComplete } = this.state;

    return (
      <div className={styles.taskAssignCss}>
        <Refresh Percentage={20} MarginBottom={20} RefreshComplete={RefreshComplete} NextDataComplete={NextDataComplete}
          onNextData={this.__HandlerNextData.bind(this)}
          onRefresh={this.__HandlerRefreshData.bind(this)}>
          <div className={styles.container}>
            {this.__TaskAssignListHtml(styles)}
          </div>
        </Refresh>
      </div>
    );
  }
}

